package no.cicero.testframework;

import no.cicero.testframework.config.Config;
import no.cicero.testframework.execution.UseCasesRunner;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public abstract class Starter {

    private static final String defaultConfigLocation = "c:\\cicero\\cicero-testframework\\cicero-testframework\\env\\cfp\\config.properties";

	public void start(Config config) throws Exception{
		List<Thread> runners = new ArrayList<Thread>();
		for(int i = 0; i < config.getNumberOfUsers(); ++i){
			runners.add(new Thread(new UseCasesRunner(config, i)));
		}

		for(Thread runner: runners)
			runner.start();

		for(Thread runner: runners)
			runner.join();

	}
    
    protected Config readConfig(String[] args) throws FileNotFoundException, IOException {
        Config config = new Config((args.length == 0)? defaultConfigLocation: args[0]);
		if(args.length > 1){
			config.getUseCaseNames().clear();
			for(int i = 1; i < args.length; ++i){
				config.getUseCaseNames().add(args[i]);
                System.out.println(args[i]);
            }
		}
		return config;
    }

    protected void printUsage(){
        System.out.println("Usage: " + Starter.class.getCanonicalName() + " <path_to_testcase>");
    }
}
